﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;

namespace Sequence
{
    public partial class FrmServiceUsage : Form
    {
        private  DbConnection m_connection;
        public FrmServiceUsage()
        {
            InitializeComponent();
        }

        public FrmServiceUsage(DbConnection connection)
        {
            InitializeComponent();
            m_connection = connection;
        }
        private DbConnection GetConnection()
        {
            return m_connection;
        }

        private void FrmServiceUsage_Load(object sender, EventArgs e)
        {
            LoadTree();
        }

        private void tvServices_DoubleClick(object sender, EventArgs e)
        {
            if (tvServices.SelectedNode != null)
            {
                string[] arrTag = tvServices.SelectedNode.Tag.ToString().Split('\t');
                if (arrTag.Count() == 2)
                {
                    if (arrTag[0] == "Process")
                    {
                        Program.mf.OpenProcess(arrTag[1]);
                    }
                }
            }
        }

        private void LoadTree()
        {
            string sSaveService = "";
            tvServices.Nodes.Clear();
            TreeNode oLastNode = tvServices.Nodes.Add("Root", "Root", "Root", "Root");
            string sSql = @"
SELECT 
    DISTINCT T_API_QD_PROCESS_STEP.query_name, T_API_QD_PROCESS.NAME, T_API_QD_PROCESS.ID
FROM 
  T_API_QD_PROCESS_STEP 
  JOIN T_API_QD_PROCESS ON T_API_QD_PROCESS.ID=T_API_QD_PROCESS_STEP.PROCESS_ID
WHERE 1=1
AND T_API_QD_PROCESS_STEP.query_name like '%$tstbFilter$%'
AND TYPE IN (1,2,14)
ORDER BY 1,2 
";
            sSql = sSql.Replace("$tstbFilter$",tstbFilter.Text);
            try
            {
                DbCommand command = GetConnection().CreateCommand();
                command.CommandText = sSql;
                command.CommandType = CommandType.Text;
                DbDataReader oDataReader = command.ExecuteReader();

                while (oDataReader.Read())
                {
                    string sService = oDataReader[0].ToString();
                    if (sService != sSaveService)
                    {
                        sSaveService = sService;
                        oLastNode = tvServices.Nodes[0].Nodes.Add(sService, sService, "Service", "Service");
                        oLastNode.EnsureVisible();
                        oLastNode.Tag = "Service\t" + sService;
                    }
                    TreeNode oNew = oLastNode.Nodes.Add("Ser-" + oDataReader[2].ToString(), oDataReader[1].ToString(), "Process", "Process");
                    oNew.Tag = "Process\t" + oDataReader[2].ToString();
                }
                oDataReader.Close();
            }
            catch (Exception ex)
            {
                //WriteMessage(ex.ToString(), "GetProcessId");
            }
        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            LoadTree();
        }
    }
}
